const domainName = getApp().globalData.domainName
const header = {'content-type':'application/x-www-form-urlencoded'}
const dataType = 'json'
const method = 'POST'
const responseType = 'text'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    
  },


  // 我的招聘
  // my_recruit(){
  //   wx.navigateTo({
  //     url:'/pages/my_recruit/my_recruit?user_id=' + this.data.user_id
  //   })
  // },
  
  // 撤销/放入草稿箱
  revoke(){
    this.update_row_status(this.data.user_id,this.data.recruitment_id,1,'')
  },
  

  // 提交审核
  submit_check(){
    this.update_row_status(this.data.user_id,this.data.recruitment_id,2,'')
  },

  // 通过
  accept(){
    this.update_row_status(this.data.user_id,this.data.recruitment_id,4,'')
  },

  // 不通过
  reject(){
    wx.showModal({
      editable: true,
      placeholderText: '拒绝原因',
      showCancel: true,
      title: '拒绝原因',
      success: res => {
        let reject_reason = res.content
        if(res.confirm){
          this.update_row_status(this.data.user_id,this.data.recruitment_id,3,reject_reason)
        }else{
          this.onShow()
        }
      },
    })
  },

  // 删除
  close(){
    this.update_row_status(this.data.user_id,this.data.recruitment_id,5,'')
  },


  // 修改
  update(){
    wx.navigateTo({
      url: '/pages/recruit_publish/recruit_publish?recruitment_info=' + JSON.stringify(this.data.recruit_info),
    })
  },


  // 更新信息状态
  update_row_status(user_id,recruitment_id,status,reject_reason){
    wx.request({
      url: domainName + '/update_recruitment_status',
      data: {
        user_id:user_id,
        recruitment_id:recruitment_id,
        status:status,
        reject_reason:reject_reason,
      },
      dataType: dataType,
      enableCache: true,
      enableHttp2: true,
      enableQuic: true,
      header: header,
      method: method,
      responseType: responseType,
      timeout: 0,
      success: res => {
        if(res.data.update_res){
          this.onShow()
          wx.showToast({
            title: '操作完成',
            duration: 1500,
            icon: 'success',
          })
        }
      },
    })
  },


  // 拨打电话
  mk_call(){
    wx.makePhoneCall({
      phoneNumber: this.data.recruit_info.contact,
    })
  },

  // 加载招聘信息
  load_recruitment(recruitment_id){
    wx.showLoading({
      title: '网络连接中',
      mask: false,
    })
    wx.request({
      url: domainName + '/load_recruitment',
      data: {
        recruitment_id:recruitment_id,
      },
      dataType: dataType,
      enableCache: true,
      enableHttp2: true,
      enableQuic: true,
      header: header,
      method: method,
      responseType: responseType,
      timeout: 0,
      success: res => {
        wx.hideLoading()
        let recruit_info = res.data.recruit_info
        this.setData({
          recruit_info:recruit_info,
          can_onshow:true,
        })

        // if(recruit_info.status==3 && recruit_info.publisher_id==this.data.user_id){
        //   wx.showModal({
        //     cancelColor: '#999',
        //     cancelText: '问管理员',
        //     confirmColor: '#00b050',
        //     confirmText: '我知道了',
        //     content: '您所发布的招聘信息不合法，请修改',
        //     showCancel: true,
        //     title: '提示',
        //     success: res => {
        //       if(res.cancel){
        //         wx.navigateTo({
        //           url: '/pages/contact_admin/contact_admin',
        //         })
        //       }
        //     },
        //   })
        // }
      },
    })
  },

  check_user_jg_admin(user_id, recruitment_id){
    wx.request({  
      url: domainName + '/check_user_jg_admin',
      data: {
        user_id:user_id,
        recruitment_id:recruitment_id,
      },
      dataType: dataType,
      enableCache: true,
      enableHttp2: true,
      enableQuic: true,
      header: header,
      method: method,
      responseType: responseType,
      timeout: 0,
      success: res => {
        this.setData({
          is_jg_admin:res.data.is_jg_admin
        })
      },
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log('options',options)
    let recruitment_id = options.recruitment_id
    let super_admin = getApp().globalData.super_admin
    let user_id = wx.getStorageSync('user_id')
    this.setData({
      user_id:user_id,
      super_admin:super_admin,
      recruitment_id:recruitment_id,
      windowWidth : wx.getSystemInfoSync().windowWidth,
    })
    this.load_recruitment(recruitment_id)
    this.check_user_jg_admin(user_id,recruitment_id)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(this.data.can_onshow){
      this.onLoad(this.options)
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: this.data.recruit_info.title,
      path: '/pages/recruitment/recruitment?recruitment_id=' + this.data.recruitment_id,
    }
  },
})